package com.mdd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdd.common.BaseContext;
import com.mdd.common.R;
import com.mdd.pojo.ShoppingCart;
import com.mdd.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加菜品或套餐购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> addDishOrSetmeal(@RequestBody ShoppingCart shoppingCart){
        //设置用户id,指定该条购物车数据属于哪个用户
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //根据菜品Id或者套餐Id来查询购物车中是否有该菜品或者套餐
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
                                //添加到购物车的是菜品
        queryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId())
                                //添加到购物车的是套餐
                        .eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId())
                        //表示在该用户购物车内
                        .eq(ShoppingCart::getUserId,shoppingCart.getUserId());

        ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
        //如果不存在则将该菜品添加到购物车，数量默认为1
        if (shoppingCart1==null) {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
        //如果存在则在原来购物车中该菜品或者套餐的基础上数量加1
        Integer number = shoppingCart1.getNumber();
        shoppingCart1.setNumber(number+1);
        shoppingCartService.updateById(shoppingCart1);
        return R.success(shoppingCart1);
    }

    /**
     * 展示购物车的菜品和套餐
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> showShoppingCart(){
        //获取当前用户id
        Long currentId = BaseContext.getCurrentId();
        //获取当前用户的购物车信息
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId).orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);

        return R.success(shoppingCarts);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> cleanShoppingCart(){
        //获取当前登录用户的id
        Long currentId = BaseContext.getCurrentId();
        //清空该用户的购物车
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(queryWrapper);
        return R.success("已清空购物车");
    }

    /**
     * 减少商品数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> subDishOrSetmeal(@RequestBody ShoppingCart shoppingCart){
        //根据菜品或套餐id和UserId查询出该用户购物车的这个菜品或套餐的信息
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId())
                .eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        //若该菜品或套餐的份数减1等于0则将该菜品或套餐从购物车中删除否者份数减一
        one.setNumber(one.getNumber()-1);
        if (one.getNumber()==0){
            shoppingCartService.removeById(one);
        }else {
            shoppingCartService.updateById(one);
        }
        return R.success(one);
    }
}
